package com.danke.banner.demo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.danke.banner.demo.R;
import com.danke.banner.demo.banner.RecyclerItemBanner;
import com.danke.banner.demo.entity.RecyclerBannerBean;
import com.danke.banner.demo.entity.RecyclerBaseItemBean;
import com.danke.banner.demo.entity.RecyclerNormalBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author wupuquan
 * @version 1.0
 * @since 2020/3/26 10:54
 */
public class RecyclerBannerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<RecyclerBaseItemBean> mList;

    public RecyclerBannerAdapter(List<RecyclerBaseItemBean> list) {
        this.mList = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        switch (viewType) {
            case RecyclerBaseItemBean.TYPE_NORMAL:
                return new NormalViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_recycler_normal, parent, false));
            case RecyclerBaseItemBean.TYPE_BANNER:
                return new BannerViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_recycler_banner, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        RecyclerBaseItemBean itemBean = mList.get(position);
        switch (viewType) {
            case RecyclerBaseItemBean.TYPE_NORMAL:
                ((NormalViewHolder) holder).setData((RecyclerNormalBean) itemBean);
                break;
            case RecyclerBaseItemBean.TYPE_BANNER:
                ((BannerViewHolder) holder).setData((RecyclerBannerBean) itemBean);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public static class NormalViewHolder extends RecyclerView.ViewHolder {

        private TextView mText;

        public NormalViewHolder(@NonNull View itemView) {
            super(itemView);
            mText = itemView.findViewById(R.id.tv_item);
        }

        public void setData(RecyclerNormalBean data) {
            mText.setText(data.text);
        }
    }

    public static class BannerViewHolder extends RecyclerView.ViewHolder {

        private RecyclerItemBanner mBanner;

        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
            mBanner = itemView.findViewById(R.id.banner);
        }

        public void setData(RecyclerBannerBean data) {
            mBanner.setData(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null && !mList.isEmpty()) {
            RecyclerBaseItemBean itemBean = mList.get(position);
            return itemBean.getItemType();
        }
        return super.getItemViewType(position);
    }
}
